package livesun.justiceeternal.view.adapter.wrapper;


import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

/**
 * 类描述：添加头布局和脚布局的装饰者adapter
 * 创建人：livesun
 * 创建时间：2017/8/21
 * 修改人：
 * 修改时间：
 * github：https://github.com/livesun
 */

public class WrapperHeadAndFootAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {

    //数据列表的adapter 不包含头部
    private RecyclerView.Adapter mAdapter;
    //头部的集合
    private SparseArray<View> mHeads=new SparseArray<>();
    //脚部的集合
    private SparseArray<View> mFoots=new SparseArray<>();
    //头部的key的起始值
    private static int HEAD_KEY=1000000;
    //脚部的key的起始值
    private static int FOOT_KEY=2000000;
    private OnHeadClickListener onHeadClickListener;
    private OnFootClickListener onFootClickListener;

    public WrapperHeadAndFootAdapter(RecyclerView.Adapter adapter){

        mAdapter = adapter;
    }

    public WrapperHeadAndFootAdapter(RecyclerView.Adapter adapter, SparseArray<View> heads, SparseArray<View> foots){
        mAdapter = adapter;
        mHeads=heads;
        mFoots=foots;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //头部
        if(mHeads.indexOfKey(viewType)>=0){

            return creatHeadAndFoot(mHeads.get(viewType));

        }else if(mFoots.indexOfKey(viewType)>=0){
            //脚部
            return creatHeadAndFoot(mFoots.get(viewType));
        }else{

            return mAdapter.onCreateViewHolder(parent,viewType);
        }
    }

    private RecyclerView.ViewHolder creatHeadAndFoot(View view) {
        return new RecyclerView.ViewHolder(view) {
        };
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int headSize = mHeads.size();
        if(position< headSize){
            if(onHeadClickListener!=null){
                final View headView = mHeads.get(mHeads.keyAt(position));
                if(headView!=null){
                    headView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onHeadClickListener.onClick(headView,getHeadPosition(headView));
                        }
                    });
                }
            }
            return;
        }

        //中间数据
        int adjPosition= position - headSize;
        int itemCount = mAdapter.getItemCount();
        if(mAdapter!=null){
            if(adjPosition< itemCount){
                 mAdapter.onBindViewHolder(holder,adjPosition);
            }
        }
        if(adjPosition>=itemCount){
            if(onFootClickListener!=null){
                final View footView = mFoots.get(mFoots.keyAt(adjPosition-itemCount));
                if(footView!=null){
                    footView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onFootClickListener.onClick(footView,getHeadPosition(footView));
                        }
                    });
                }
            }

            return;
        }
    }


    @Override
    public int getItemCount() {
        return mAdapter.getItemCount()+mHeads.size()+mFoots.size();
    }

    @Override
    public int getItemViewType(int position) {
        int headSize = mHeads.size();
        //头部
        if(position< headSize){

            return mHeads.keyAt(position);
        }
        //中间数据
        int adjPosition= position - headSize;
        int itemCount = mAdapter.getItemCount();
        if(mAdapter!=null){
            if(adjPosition< itemCount){
                return mAdapter.getItemViewType(adjPosition);
            }
        }
        //底部
        int footPosition = adjPosition - itemCount;
        return mFoots.keyAt(footPosition);
    }

    /**
     * 添加头部布局
     * @param view
     */
    public void addHeadView(View view){

        if(mHeads.indexOfValue(view)==-1){
            int position = HEAD_KEY++;
            mHeads.put(position,view);
        }

    }

    /**
     * 添加脚部布局
     * @param view
     */
    public void addFootView(View view){
        if(mFoots.indexOfValue(view)==-1){
            mFoots.put(FOOT_KEY++,view);
        }
    }

    /**
     * 移除头部
     * @param view
     */
    public void removeHeadView(View view){
        if(mHeads.indexOfValue(view)>=0){
            mHeads.removeAt(mHeads.indexOfValue(view));
            notifyDataSetChanged();
        }
    }

    public void removeHeadView(View view,int position){
        if(mHeads.indexOfValue(view)>=0){
            mHeads.removeAt(mHeads.indexOfValue(view));
            notifyItemRemoved(position);
        }
    }
    /**
     * 移除脚部
     * @param view
     */
    public void removeFootView(View view){
        if(mFoots.indexOfValue(view)>=0){
            mFoots.removeAt(mFoots.indexOfValue(view));
        }
    }

    public void removeFootView(View view,int position){
        if(mFoots.indexOfValue(view)>=0){
            mFoots.removeAt(mFoots.indexOfValue(view));
            notifyItemRemoved(position);
        }
    }

    /**
     * 获取头部的位置
     * @param view
     * @return
     */
    public int getHeadPosition(View view) {
        return  mHeads.indexOfValue(view);
    }

    /**
     * 获取底部的位置
     */

    public int getFootPosition(View view){
        int position=mFoots.indexOfValue(view)+mHeads.size()+mAdapter.getItemCount();
        return position;
    }


    /**
     * 设置头部的点击事件
     * @param onHeadClickListener
     */
    public void setOnHeadClickListener(OnHeadClickListener onHeadClickListener){

        this.onHeadClickListener = onHeadClickListener;
    }

    /**
     * 设置底部的点击事件
     */

    public void setOnFootClickListener(OnFootClickListener onFootClickListener){

        this.onFootClickListener = onFootClickListener;
    }

    /**
     * 解决GridLayoutManager添加头部和底部不占用一行的问题
     */
    public void spanSize(RecyclerView recycler) {
        if (recycler.getLayoutManager() instanceof GridLayoutManager) {
            final GridLayoutManager layoutManager = (GridLayoutManager) recycler.getLayoutManager();
            layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    boolean isHeaderOrFooter = isHeaderPosition(position) || isFooterPosition(position);
                    return isHeaderOrFooter ? layoutManager.getSpanCount() : 1;
                }
            });
        }
    }

    /**
     * 是不是底部位置
     */
    private boolean isFooterPosition(int position) {
        return position >= (mHeads.size() + mAdapter.getItemCount());
    }

    /**
     * 是不是头部位置
     */
    private boolean isHeaderPosition(int position) {
        return position < mHeads.size();
    }


}
